﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using ShopB2C.Entity;
using ShopB2C.Parameters;

namespace ShopB2C.Helper {

    /// <summary>
    /// 创建分类前台展示
    /// </summary>
    public class ClassifyMenuHelper {
        private const string _filePath = "~/Scripts/Website/ClassifyMenuHtml.js";
        private StringBuilder _sb;
        private string _url;
        private List<ClassifyEntity> _classesList;
        private List<int> _pList;

        public ClassifyMenuHelper(List<ClassifyEntity> classesList, string url) {
            this._sb = new StringBuilder();
            this._url = url;
            this._classesList = classesList;
            this._pList = new List<int>() { 0 };
        }

        /// <summary>
        /// 创建文件
        /// </summary> 
        public void Create() {
            if (this._classesList != null) {
                this.Recursion(0, "m");
            }

            string s = string.Format(
                "$(function() {{ var m = new MenuClass(); m.Apply(); {0} }});",
                this._sb.ToString());
            try {
                File.WriteAllText(HttpContext.Current.Server.MapPath(_filePath), s, Encoding.UTF8);
            }
            catch (Exception ex) {
                throw new Exception("写入文件失败，可能是没有赋予正确的权限！");
            }
        }
        // 递归加入
        private void Recursion(int parentClassifyId, string parentJSObject) {
            const string item = "var {0} = {1}.Item('{2}','{3}');";
            const string link = "{0}.Link('{1}', '{2}');";

            var curList = this._classesList.Where(c => {
                return c.ParentClassifyId == parentClassifyId;
            });
            foreach (ClassifyEntity ce in curList) {

                List<int> cList = new List<int>();
                if (!string.IsNullOrEmpty(ce.Path)) {
                    cList = ce.Path.Split('-').ToList().ConvertAll<int>(p => { return int.Parse(p); });
                }
                cList.Insert(0, 0);
                cList.Add(ce.ClassifyId);
                ProductParam ppClassify = new ProductParam() {
                    Classify = cList,
                    PropertyVal = this._pList
                };
                if (ce.Childs > 0) {
                    this._sb.AppendFormat(
                        item + Environment.NewLine,
                        string.Format("i{0}", ce.ClassifyId),
                        parentJSObject,
                        ce.Title.Replace("'", "\\'"),
                        this._url.Replace("-pp-", ppClassify.ToString()));
                    this.Recursion(ce.ClassifyId, string.Format("i{0}", ce.ClassifyId));
                }
                else {
                    this._sb.AppendFormat(
                        link + Environment.NewLine,
                        parentJSObject,
                        ce.Title.Replace("'", "\\'"),
                        this._url.Replace("-pp-", ppClassify.ToString()));
                }
            }
        }
    }
}
